package org.jeecg.modules.demo.mes.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.modules.demo.mes.controller.query.ProductionOnlineReplacementQuery;
import org.jeecg.modules.demo.mes.entity.MesMaterialDetail;
import org.jeecg.modules.demo.mes.mapper.MesProductionOnlineReplacementMapper;
import org.jeecg.modules.demo.mes.service.IMesMaterialDetailsService;
import org.jeecg.modules.demo.mes.service.IMesProductionOnlineReplacementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class MesProductionOnlineReplacementService extends ServiceImpl<MesProductionOnlineReplacementMapper, Map> implements IMesProductionOnlineReplacementService {
    @Autowired
    private IMesMaterialDetailsService mesMaterialDetailsService;
    @Override
    public List<Map<String, Object>> queryProductionOnlineReplacementList(ProductionOnlineReplacementQuery query) {
        if (query==null){
            return new ArrayList<>();
        }
        return this.baseMapper.queryProductionOnlineReplacementList(query);
    }

    @Override
    public Integer queryProductionOnlineReplacementCount(ProductionOnlineReplacementQuery query) {
        if (query==null){
            return 0;
        }
        return this.baseMapper.queryProductionOnlineReplacementCount(query);
    }

    @Override
    @Transactional(readOnly = false,isolation = Isolation.DEFAULT,propagation = Propagation.REQUIRED)
    public void replaceMaterialBarCode(ProductionOnlineReplacementQuery query) {
        final String materialBarCodeOld = query.getMaterialBarCodeOld();
        final String materialBarCodeNew = query.getMaterialBarCodeNew();
        if (StringUtils.isBlank(materialBarCodeNew) || StringUtils.isBlank(materialBarCodeOld)){
            throw new JeecgBootException("物料号不正确，请修正后再操作");
        }
        final MesMaterialDetail detailOld = mesMaterialDetailsService.queryMaterialDetailByMaterialBarCode(materialBarCodeOld);
        if (detailOld==null){
            throw new JeecgBootException("旧物料号有误，请刷新页面后重试");
        }
        final String userName = JwtUtil.getUserName();
        final Date now = new Date();
        final String workOrderDetailId = detailOld.getWorkOrderDetailId();
        final String procedureId = detailOld.getProcedureId();

        final MesMaterialDetail detailNew = mesMaterialDetailsService.queryMaterialDetailByMaterialBarCode(materialBarCodeNew);
        if (detailNew==null){
            throw new JeecgBootException("新物料号有误，请检查后重新操作");
        }
        if (StringUtils.equals("2",detailNew.getMaterialStatus())){
            throw new JeecgBootException("新物料号已经被使用了，请检查后重新操作");
        }
        //更新旧的
        detailOld.setProcedureId(null);
        detailOld.setWorkOrderDetailId(null);
        detailOld.setMaterialStatus("1");
        detailOld.setUpdateTime(now);
        detailOld.setUpdateBy(userName);
        mesMaterialDetailsService.updateById(detailOld);
        //更新新的
        detailNew.setUpdateBy(userName);
        detailNew.setUpdateTime(now);
        detailNew.setWorkOrderDetailId(workOrderDetailId);
        detailNew.setMaterialStatus("2");
        detailNew.setProcedureId(procedureId);
        mesMaterialDetailsService.updateById(detailNew);
    }
}
